package br.tce.ufs.appcoleta_mobile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.widget.ImageView;

;

public class CaptureFoto extends Activity {

	private static final int CAMERA_CODE = 0;
    private ImageView ivFoto;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capturefoto);
		ivFoto = (ImageView)findViewById(R.id.ivFoto);
		cameraIntent();

	}

	public void clikCancelar(View view){
		cameraIntent();
	}
	
	private void cameraIntent() {
		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(this)));
		startActivityForResult(intent, CAMERA_CODE);
	}

	private File getFile(Context context) {
		final File path = new File(Environment.getExternalStorageDirectory(),
				context.getPackageName());
		if (!path.exists()) {
			path.mkdir();
		}
		return new File(path, "image.jpeg");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == CAMERA_CODE) {
			final File file = getFile(this);
			try {
				Bitmap captureBmp = Media.getBitmap(getContentResolver(),
						Uri.fromFile(file));
				ivFoto.setImageBitmap(captureBmp);
				// do whatever you want with the bitmap (Resize, Rename, Add To
				// Gallery, etc)
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
